$(function(){
				
        	 var strs = window.location.href.split(":");
             var root = strs[0]+":"+strs[1];
             var str2s = strs[2].split("/");
             var rootpath = root+":"+str2s[0]+"/"+str2s[1]+"/";
        	
             $("#hidebt").on("click",function(){
                
                $("#di").animate({right:"-500px"},300,'linear',callback);
             });
             
            
            $("#showbt").on("click",function(){
                $("#di").animate({right:"0px"},300,'linear',callback1);
                $("#showbt").hide();
             });
             function callback1(){
                  $("#di").show();
                  $.ajax({
                    type:"GET",
                    url:rootpath+"find",
                    dataType:"json"
                })
                .done(function(msg){
                   var op = cartTemplate($("table"),msg)
                    op.createTable();
                })
             }
            function callback(){
                $("#showbt").show();
                $("#di").hide();
             }
            function cartTemplate(cart,data){
                var obj = [];
                obj.createTable=function(){
                	$("#cart_body ul").empty();
                	data.forEach(function(item){
                		console.log(item);
                		$("#pro_sample").clone(true)
                    	.css("display","block")
                    	.find(".pro_name").text(item.food.fname).end()
                    	.find("#unit_price").text(item.food.fprice).end()
                    	.find("#delImage").attr("fid",item.food.fid).end()
                    	.find("#minusId").attr("fid",item.food.fid).end()
                    	.find("#plusId").attr("fid",item.food.fid).end()
                    	.find("#countId").attr("value",item.count).end()
                    	.find("#subtotal").text((item.food.fprice)*(item.count)).end()
                    	.appendTo("#cart_body ul");
                	});
                        calSum();
                }                
                return obj;
            }
            $("#minusId").on("click",function(){
            	var fid = $(this).attr("fid");
            	var count = $(this).next().val();
            	var type = 1;
            	if(count!=0){
            		update(fid,type);
            	}
            })
            $("#plusId").on("click",function(){
            	var fid = $(this).attr("fid");
            	var type=2;
            	update(fid,type);
            });
            $(".delImage").on("click",function(){
            	var fid = $(this).attr("fid");
            	deleteItem(fid);
            });
            $(".order").on("click",function(){
            	var icnum = $("#merchantFlag").attr("icnum");
            	var fid = $(this).attr("fid");
                var div = $(this).parent().parent();
                var x=div.offset().left;
                var y=div.offset().top;
                var cartt=$("#showbt");
                var cartt1=$("#di");
                var lis = div.children();

                var cl=cartt.offset().left;
                var ct=cartt.offset().top;
                if(cl==0&&ct==0){
                  cl= cartt1.offset().left;
                  ct=cartt1.offset().top;
                }
                
                 var div1 =div.clone().css("position","absolute").css("left",x).css("top",y);
                div1.appendTo(div.parent());
                div1.animate({left:cl,top:ct,opacity:0},800,'linear',function(){
                   addOrder(icnum,fid);
                  $(this).hide();
                });
               
            });
            
            
         
            //计算所有
            function calSum(){
                var sum = 0;
                $(".cart_pro_price").each(function(){//统计总价
                    var tdSub = $(this).text();
                    sum += parseInt(tdSub);
                });
                $("#priceTotal").text(sum);
            }
            function addOrder(icnum,fid){
            	
                $.ajax({
                    type:"GET",
                    url:rootpath+"plusCount?icnum="+icnum+"&fid="+fid
                }).done(function(msg){
                    if(msg==""){
                    	 swal({
                    		  title: "您还没有登录",
                    		  text: "请选择登录还是继续浏览菜品",
                    		  type: "warning",
                    		  showCancelButton: true,
                    		  confirmButtonColor: "#00ff00",
                    		  confirmButtonText: "前往登录界面登录",
                    		  closeOnConfirm: false
                    		},
                    		function(){
                    			window.location.href="tologin";
                    		});
                    	
                    	
                    	
                    }else{
	                	var op = cartTemplate($("#cart_body"),msg);
	                    op.createTable();
                    }
                	
                    
                });
            }
            
            function update(fid,type){
                $.ajax({
                    type:"GET",
                    url:rootpath+"updateCount?fid="+fid+"&type="+type 
                }).done(function(msg){
                    var op = cartTemplate($("#cart_body"),msg);
                    op.createTable();
                });
            }
            function deleteItem(fid){
            	swal({
          		  title: "你确定要删除该商品吗？",
          		  text: "是否选择删除该商品",
          		  type: "warning",
          		  showCancelButton: true,
          		  confirmButtonColor: "#00ff00",
          		  confirmButtonText: "删除",
          		  closeOnConfirm: false
          		},
          		function(){
          			 $.ajax({
                         type:"GET",
                         url:rootpath+"deleteItem?fid="+fid
                     }).done(function(msg){
                         var op = cartTemplate($("#cart_body"),msg);
                         op.createTable();
                         swal("Deleted!", "删除商品成功！", "success");
                     });
          		});
               
            }
            
            $(".clear").on("click",function(){
            	$.ajax({
                    type:"GET",
                    url:rootpath+"remove" 
                }).done(function(msg){
                    var op = cartTemplate($("#cart_body"),msg);
                    op.createTable();
                });
            	
            });

            $("#settlementId").on("click",function(){
            	var totalMoney = $("#priceTotal").text();
            	if(totalMoney==0){
            		swal("购物车不能为空！！");
            	}else{
            		var subUrl = $("#settlementId").attr("href");
            		var url = subUrl+"&priceTotal="+totalMoney;
            		window.location.href=url;
            	}
            })
            
            $.ajax({
                type:"GET",
                url:rootpath+"findAllFoodInit"
            }).done(function(msg){
                var op = cartTemplate($("#cart_body"),msg);
                op.createTable();
            });
            
            
        });